package operate;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书！");
        Scanner in = new Scanner(System.in);
        System.out.print("输入书名：");
        String name = in.nextLine();
        System.out.print("输入作者：");
        String author = in.nextLine();
        System.out.print("输入类型：");
        String type = in.nextLine();
        System.out.print("输入价格：");
        int price = in.nextInt();

        //判断图书是否已存在
        for (int i = 0; i < bookList.getUsedSize(); i++) {
            if (name.equals(bookList.getBook(i).getName())) {
                System.out.println("添加失败，"+name+"已存在！");
                return;
            }
        }

        //存入数组
        bookList.setBook(new Book(name, author, type, price));
        //修改UsedSize
        bookList.setUsedSize(bookList.getUsedSize() + 1);
    }
}
